package now_coder.two_pointer.ncs;

import java.util.HashMap;
import java.util.Map;

public class NC41 {

    public int maxLength (int[] arr) {
        if (arr == null || arr.length == 0)
            return 0;

        int low = 0;
        int high = 0;
        int maxLen = 0;
        Map<Integer, Integer> map = new HashMap<>();

        while (high < arr.length){
            if (high > 0 && map.containsKey(arr[high])){
                maxLen = Math.max(maxLen, high - low);
                low = Math.max(map.get(arr[high]) + 1, low);
            }

            map.put(arr[high], high);
            high++;
        }

        return Math.max(maxLen, high - low);
    }

    public static void main(String[] args) {
        System.out.println(new NC41().maxLength(new int[]{2, 3, 4, 2, 2, 2, 3, 7, 4, 5}));
        System.out.println(new NC41().maxLength(new int[]{2, 2, 3, 4, 5}));
        System.out.println(new NC41().maxLength(new int[]{2, 2, 1, 7 ,3 , 8, 6, 2, 3, 4, 5, 6, 5}));
    }

}
